home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CPPTASK.ARJ / TSKCNT.CPP < prev    next >
C/C++ Source or Header  |  1991-08-21  |  3KB  |  165 lines

  1. /*
  2.    CPPTask - A Multitasking Kernel For C++
  3.  
  4.    Version 1.0 08-12-91
  5.  
  6.    Ported by Rich Smith from:
  7.  
  8.    Public Domain Software written by
  9.       Thomas Wagner
  10.       Patschkauer Weg 31
  11.       D-1000 Berlin 33
  12.       West Germany
  13.  
  14.    TSKCNT.CPP - Counter handling routines.
  15.  
  16.    Subroutines:
  17.         counter::counter
  18.         counter::~counter
  19.         counter::set_flag
  20.         counter::clear_counter
  21.         counter::wait_counter_set
  22.         counter::wait_counter_clear
  23.         counter::inc_counter
  24.         counter::check_counter
  25.  
  26. */
  27.  
  28. #include <stdio.h>
  29.  
  30. #include "task.hpp"
  31. #include "tsklocal.hpp"
  32.  
  33. /*
  34.    counter - initialises counter.
  35. */
  36.  
  37. counter::counter ()
  38. {
  39.    flags = 0;
  40.  
  41.    wait_set = wait_clear = NULL;
  42.    state = 0;
  43. }
  44.  
  45.  
  46. /*
  47.    ~counter - kills all processes waiting for counter
  48. */
  49.  
  50. counter::~counter ()
  51. {
  52.    CRITICAL;
  53.  
  54.    C_ENTER;
  55.    tsk_kill_queue (&wait_set);
  56.    tsk_kill_queue (&wait_clear);
  57.    state = 0L;
  58.    C_LEAVE;
  59.  
  60. }
  61.  
  62.  
  63. /*
  64.    clear_counter  - Sets counter to zero. All tasks waiting for
  65.                     Counter zero are made eligible.
  66. */
  67.  
  68. void far counter::clear_counter (void)
  69. {
  70.    CRITICAL;
  71.  
  72.    C_ENTER;
  73.    state = 0L;
  74.    while (wait_clear != NULL)
  75.       wait_clear = wait_clear->tsk_runable ();
  76.    C_LEAVE;
  77. }
  78.  
  79. /*
  80.    wait_counter_set  - Wait until counter is != 0. If counter is != 0 on
  81.                        entry, the counter is decremented and the task
  82.                        continues to run. If the counter is decremented to 
  83.                        zero, tasks waiting for zero are made eligible.
  84. */
  85.  
  86. int far counter::wait_counter_set (dword timeout)
  87. {
  88.    CRITICAL;
  89.  
  90.    C_ENTER;
  91.    if (state)
  92.       {
  93.       if (!--state)
  94.          while (wait_clear != NULL)
  95.             wait_clear = wait_clear->tsk_runable ();
  96.       C_LEAVE;
  97.       return 0;
  98.       }
  99.  
  100.    tsk_current->set_retptr(NULL);
  101.    tsk_wait (&wait_set, timeout);
  102.    return (int)tsk_current->get_retptr();
  103. }
  104.  
  105. /*
  106.    wait_counter_clear - Wait until counter is == 0. If counter is == 0 on
  107.                        entry, the task continues to run.
  108. */
  109.  
  110. int far counter::wait_counter_clear (dword timeout)
  111. {
  112.    CRITICAL;
  113.  
  114.    C_ENTER;
  115.    if (!state)
  116.       {
  117.       C_LEAVE;
  118.       return 0;
  119.       }
  120.  
  121.    tsk_current->set_retptr(NULL);
  122.    tsk_wait (&wait_clear, timeout);
  123.    return (int)tsk_current->get_retptr();
  124. }
  125.  
  126.  
  127. /*
  128.    inc_counter - Increment counter. If there are tasks waiting for the
  129.                  set state, the first task in the queue is made eligible.
  130. */
  131.  
  132. void far counter::inc_counter (void)
  133. {
  134.    tcbptr curr;
  135.  
  136.    CRITICAL;
  137.  
  138.    C_ENTER;
  139.    if ((curr = wait_set) == NULL)
  140.       {
  141.       state++;
  142.       C_LEAVE;
  143.       return;
  144.       }
  145.    wait_set = curr->tsk_runable ();
  146.    C_LEAVE;
  147. }
  148.  
  149.  
  150. /*
  151.    check_counter - return current counter state.
  152. */
  153.  
  154. dword far counter::check_counter (void)
  155. {
  156.    return state;
  157. }
  158.  
  159.  
  160.  
  161. void far asm_inc_counter(counterptr cnt)
  162. {
  163.    cnt->inc_counter();
  164. }
  165.